package com.jf3q.front.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.jf3q.common.domain.EduComment;
import com.jf3q.common.dto.CommonDto;
import com.jf3q.common.service.impl.EduCommentServiceImpl;
import com.jf3q.common.vo.ResultVo;
import com.jf3q.common.domain.EduCommentReply;
import com.jf3q.common.service.EduCommentReplyService;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/comment")
public class CommentController {
    @Autowired
    EduCommentServiceImpl eduCommentService;
    @Autowired
    EduCommentReplyService eduCommentReplyService;
    @GetMapping("/get/{rating}")
    @Operation(summary = "评论测试")
    public ResultVo getList(@PathVariable String rating,
     @RequestParam(defaultValue = "1")Integer  pageindex){
        Page<EduComment> page = eduCommentService.getPage(pageindex,rating);
        return  ResultVo.success("",page);
    }
    @PostMapping("seave")
    public ResultVo Insert(@RequestBody EduComment commentDto){
        boolean save = eduCommentService.save(commentDto);
        if (save){
            return ResultVo.success("发表成功！",null);
        }else{
            return  ResultVo.error("发表失败！");
        }
    }
    @PostMapping("upload")
    public ResultVo upload(@RequestBody EduCommentReply eduCommentReply){
        boolean save = eduCommentReplyService.save(eduCommentReply);
        if (save){
            return ResultVo.success("回复成功!！",null);
        }else{
            return  ResultVo.error("回复失败!");
        }
    }
    @GetMapping("one/{id}")
    public ResultVo getone(@PathVariable String  id){
        try {
            List<EduComment> one = eduCommentService.list(new LambdaQueryWrapper<EduComment>().eq(EduComment::getMemberId, id));
            if (one!=null){
                return ResultVo.success("找到了",one.get(0));
            }else{
                return  ResultVo.error("没找到");
            }
        } catch (Exception e) {
            return  ResultVo.error("没找到");
        }
    }
    @GetMapping("ratCount")
    public ResultVo ratCount(){
        List<String> list=new ArrayList();
        list.add("good");
        list.add("average");
        list.add("bad");
        CommonDto commonDto=new CommonDto();
        for (String s : list) {
           Integer count= eduCommentService.selectRating(s);
           commonDto.getList().add(count);
        }
        return ResultVo.success("",commonDto);
    }
}
